//
//  SESettingViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/15.
//

import UIKit

class SESettingViewModel: SEBaseViewModel {

    var uploadVM = SEUploadViewModel(withLoading: true)

    var uploadHandler: ((SEHttpStatus) -> Void)?

    var userNickHandler: ((SEHttpStatus) -> Void)?

    var logoutHandler: ((SEHttpStatus) -> Void)?

    var avatorUrl: String = ""
    var avatorSourceId: String = ""
    var userNick: String = ""

    // APIRequest

    func uploadImg(image: UIImage) {
        uploadVM.uploadImage(bucket: "C", type: "C-1", image: image, size: image.imageSize())
        uploadVM.updateData = { [weak self] status in
            switch status {
            case .success:
                self?.avatorUrl = self?.uploadVM.fileModel?.shareUrl ?? ""
                self?.avatorSourceId = self?.uploadVM.fileModel?.resourceId ?? ""
            default:
                break
            }
            self?.uploadHandler?(status)
        }
    }

    func uploadUserAvator() {
        SEHttpManager.shared.requestForAny(SEProfileAPI.userAvatar(avatar: avatorSourceId), withLoading: true) { result, status in
            switch status {
            case .success:
                UserDefaults.userAvatar = self.avatorUrl
            default: break
            }
            self.updateData?(status)
        }
    }

    func submitUserNick() {
        SEHttpManager.shared.requestForAny(SEProfileAPI.userNick(nickName: userNick), withLoading: true) { result, status in
            switch status {
            case .success:
                UserDefaults.userNick = self.userNick
            default: break
            }
            self.userNickHandler?(status)
        }
    }

    func logout() {
        SEHttpManager.shared.requestForAny(SEAuthAPI.logout, withLoading: true) { result, status in
            self.logoutHandler?(status)
        }
    }
}


